package ysq.xyg.controller;

import com.github.pagehelper.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ysq.xyg.model.*;
import ysq.xyg.model.info.BaseUserInfo;
import ysq.xyg.service.BaseMenuService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author yu sheng qiang
 * @create 2017-03-25 8:24
 **/
public class BaseController {
    private static Logger log= LoggerFactory.getLogger(BaseController.class);
    //从Session中获取值
    public static Object SessionAcceptObject(HttpServletRequest request,String name){
        Object obj=request.getSession().getAttribute(name);
        log.info("session accept "+name+" value is "+ obj);
        return obj;
    }
    //从Session中删除
    public static void SessionDeleteObject(HttpServletRequest request,String name){
        request.getSession().removeAttribute(name);
        log.info("session delete "+name);
    }
    //从Session中修改/添加
    public static void SessionAddOrUpdateObject(HttpServletRequest request,String name,Object value){
        request.getSession().setAttribute(name,value);
        log.info("session AddOrUpdate "+name+" value is "+value);
    }
    //从Cookies中获取值
    public static String CookiesAcceptObject(HttpServletRequest request,String name){
        String value=null;
        Cookie[] cookies = request.getCookies();//这样便可以获取一个cookie数组
        if (cookies != null && !"".equals(cookies) && cookies.length > 0) {
            for(Cookie cookie : cookies){
                if(cookie.getName().equals(name.trim())){
                    value = cookie.getValue();
                    break;
                }
            }
        }
        log.info("Cookies Accept "+name+" value is "+value);
        return  value;
    }
    //从Cookies中删除
    public static void CookiesDeleteObject(HttpServletRequest request,HttpServletResponse response,String name){
        Cookie[] cookies = request.getCookies();
        if (cookies != null && !"".equals(cookies) && cookies.length > 0) {
            for(Cookie cookie : cookies){
                if(cookie.getName().equals(name)){
                    cookie.setValue(null);
                    cookie.setMaxAge(0);// 立即销毁cookie
                    cookie.setPath("/");
                    response.addCookie(cookie);
                    log.info("Cookies Delete "+name);
                    break;
                }
            }
        }else{
            log.info("Cookies Delete "+name+" is no exist");
        }
    }
    //从Cookies中添加
    public static void CookiesAddObject(HttpServletRequest request,HttpServletResponse response, String name,String value){
        Cookie cookie = new Cookie(name.trim(),value.trim());
        cookie.setMaxAge(7*24*60*60);//保存一周
        cookie.setPath("/");
        response.addCookie(cookie);
        log.info("Cookies Add "+name+" value is "+value);
    }
    //从Cookies中修改
    public static void CookiesUpdateObject(HttpServletRequest request,HttpServletResponse response,String name,String value){
        Cookie[] cookies = request.getCookies();
        if (cookies != null && !"".equals(cookies) && cookies.length > 0) {
            for(Cookie cookie : cookies){
                if(cookie.getName().equals(name)){
                    log.info("Cookies Update "+name+" value is old "+cookie.getValue());
                    cookie.setValue(value);
                    cookie.setMaxAge(7*24*60*60);//保存一周
                    cookie.setPath("/");
                    response.addCookie(cookie);
                    log.info("Cookies Update "+name+" value is news "+cookie.getValue());
                    break;
                }
            }
        }
    }
    //头部标题
    public static String acceptTitle(String menuid, BaseMenuService baseMenuService){
        if(StringUtil.isEmpty(menuid)){
            return "";
        }
        BaseMenu baseMenu = baseMenuService.selectByPrimaryKey(menuid);
        String headTitle="";
        if(baseMenu!=null && StringUtil.isNotEmpty(baseMenu.getParentmenuid())){
            BaseMenu baseMenuParent = baseMenuService.selectByPrimaryKey(baseMenu.getParentmenuid());
            if(baseMenuParent==null){
                return "";
            }
            headTitle= BaseConstant.HEAD_TITLE_BASE_CONTENT.replace("TITLE",baseMenuParent.getMenuname()).replace("HREF",BaseConstant.HEAD_TITLE_HREF_CONTENT);
            headTitle=headTitle+BaseConstant.HEAD_TITLE_CURRENT_CONTENT.replace("TITLE",baseMenu.getMenuname());
        }
        headTitle=BaseConstant.HEAD_TITLE.replace("CONTENT",headTitle);
        return headTitle;
    }
    //头部标题
    public static String acceptTitle(String menuid, BaseMenuService baseMenuService,String name){

        BaseMenu baseMenu = baseMenuService.selectByPrimaryKey(menuid);
        String headTitle="";
        if(baseMenu!=null && StringUtil.isNotEmpty(baseMenu.getParentmenuid())){
            BaseMenu baseMenuParent = baseMenuService.selectByPrimaryKey(baseMenu.getParentmenuid());
            headTitle= BaseConstant.HEAD_TITLE_BASE_CONTENT.replace("TITLE",baseMenuParent.getMenuname()).replace("HREF",BaseConstant.HEAD_TITLE_HREF_CONTENT);
            String url=BaseConstant.HEAD_TITLE_HREF_CONTENT;
            if(baseMenu.getMenuurl().contains("?")){
                url=baseMenu.getMenuurl()+"&menuid="+menuid;
            }else{
                url=baseMenu.getMenuurl()+"?menuid="+menuid;
            }
            headTitle=headTitle+BaseConstant.HEAD_TITLE_BASE_CONTENT.replace("TITLE",baseMenu.getMenuname()).replace("HREF","../"+url);
            headTitle=headTitle+BaseConstant.HEAD_TITLE_CURRENT_CONTENT.replace("TITLE",name);
        }
        headTitle=BaseConstant.HEAD_TITLE.replace("CONTENT",headTitle);
        return headTitle;
    }
    //获取当前登录用户信息
    public static BaseUserInfo acceptCurrentUser(HttpServletRequest request){
        return (BaseUserInfo) request.getSession().getAttribute(BaseConstant.SESSION_USERINFO);
    }




}
